package com.example.zjsr.service.impl;

import com.example.zjsr.dao.anchor.AnchorMapper;
import com.example.zjsr.domain.Admin.anchors.Anchors;
import com.example.zjsr.domain.Admin.anchors.Trait;
import com.example.zjsr.service.AnchorsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lvyongwei
 */
@Service
public class AnchorsServiceImpl implements AnchorsService {

    @Autowired
    private AnchorMapper anchorMapper;

    /**
     * 添加主播信息
     * @param anchors
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertAnchors(List<Anchors> anchors) {
        /**
         * 删除关联表中的内容
         */
        anchorMapper.deleteAnchors();
        anchorMapper.deleteAnchorsTraits();
        return anchorMapper.insertAnchors(anchors);
    }

    /**
     * 添加特点内容
     * @param trait
     * @return
     */
    @Override
    public boolean insertTraits(Trait trait) {
        return anchorMapper.insertTraits(trait);
    }



    /**
     * 修改特点内容
     * @param trait
     * @return
     */
    @Override
    public boolean updateTraits(Trait trait) {
        return anchorMapper.updateTraits(trait);
    }

    /**
     * 查询特点内容是否存在
     * @param trait
     * @return
     */
    @Override
    public Trait selectByName(Trait trait) {
        return anchorMapper.selectByName(trait);
    }

    /**
     * 删除特点内容
     * @param traitIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTraits(List<Integer> traitIds) {
        anchorMapper.deleteTraits(traitIds);
        /**
         * 删除中间表内容
         */
        for(Integer id:traitIds){
            anchorMapper.deleteAnchorsTrait(id);
        }
    }

    /**
     * 修改中间表内容
     * @param anchorId
     * @param traitIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAnchorsTrait(Integer anchorId, List<Integer> traitIds) {
        anchorMapper.deleteAnchorsTrait2(anchorId);
        anchorMapper.insertAnchorsTrait(anchorId,traitIds);
    }

    /**
     * 查询所有主播信息
     * @return
     */
    @Override
    public PageInfo<Anchors> selectAnchors(Integer pageNum, Integer pageSize) {
        /**
         * 分页
         */
        PageHelper.startPage(pageNum,pageSize);
        List<Anchors> anchors = anchorMapper.selectKnowledge();
        anchors.forEach(
                anchor -> {
                    anchor.setTraitList(anchorMapper.selectTraits(anchor.getAnchorId()));
                }
        );
        PageInfo<Anchors> pageInfo = new PageInfo<>(anchors);
        return pageInfo;
    }

    /**
     * 查询所有特点内容
     * @return
     */
    @Override
    public List<Trait> selectTraits() {
        return anchorMapper.selectAllTraits();
    }
}
